package com.yanqu.road.server.logic.recharge.limit;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.enums.eWelfareActivityType;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.MallActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.webservice.GamePublicFunction;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class FundMallLimit extends BaseRMBMallLimit {
    private GamePlayer player;
    public FundMallLimit(long userId, MallInfo mallInfo) {
        super(userId, mallInfo);
    }

    @Override
    protected int pretreatment() {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if(null == player){
            return GameErrorCode.E_MALL_LIMIT_BUY;
        }
        this.player = player;
        return super.pretreatment();
    }

    @Override
    protected boolean isSystemUnLock() {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (mallInfo.getMallId() == GameConfig.ACTIVITY_PATRONS_FUND) {//门客基金
            return SystemOpenMgr.systemOpen(player, eSystemId.PatronsAbilityActivity.getValue());
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_EARNMONEY_FUND) {//赚钱基金
            return SystemOpenMgr.systemOpen(player, eSystemId.EarnSpeedFundActivity.getValue());
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_EARNMONEY_PLUS_FUND) {//赚钱精进基金
            return SystemOpenMgr.systemOpen(player, eSystemId.EarnSpeedFundPlusActivity.getValue());
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_BEAUTY_FUND) {//美女基金
            return SystemOpenMgr.systemOpen(player, eSystemId.BeautyFund.getValue());
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_BEAUTY_HIGHER_FUND) {//倾城基金
            return SystemOpenMgr.systemOpen(player, eSystemId.BeautyHigherFund.getValue());
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_CRICKET_FUND) {//促织基金
            return SystemOpenMgr.systemOpen(player, eSystemId.CricketFund.getValue());
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_PATRON_LEVEL_FUND){
            return SystemOpenMgr.systemOpen(player, eSystemId.PatronLevelFund.getValue());
        } else {
            return false;
        }
    }

    @Override
    protected LimitBuyResult buyCountLimit() {
        LimitBuyResult limitBuyResult = new LimitBuyResult();
        limitBuyResult.setLimitCount(1);
        return limitBuyResult;
    }

    @Override
    protected int logicCheck() {
        if (mallInfo.getMallId() == GameConfig.ACTIVITY_PATRONS_FUND) {//门客基金
            if(NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(), eWelfareActivityType.PatronsFund.getValue()).size() <= 0){//没开活动
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            long patronsFundMark = GamePublicFunction.getPatronsFundMark(player, userId);
            if (1 == patronsFundMark) {//限购，门客基金已购买
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_EARNMONEY_FUND) {//赚钱基金
            if(NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(), eWelfareActivityType.EarnMoneyFund.getValue()).size() <= 0){//没开活动
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            long patronsFundMark = GamePublicFunction.getPatronsFundMark(player, userId);
            if (1 != patronsFundMark) {//未买门客基金
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
            long earnSpeedFundMark = GamePublicFunction.getEarnSpeedFundMark(player, userId);
            if (1 == earnSpeedFundMark) {//限购，赚钱基金已购买
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_EARNMONEY_PLUS_FUND) {//赚钱精进基金
            if(NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(), eWelfareActivityType.EarnMoneyPlusFund.getValue()).size() <= 0){//没开活动
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            long earnSpeedFundMark = GamePublicFunction.getEarnSpeedFundMark(player, userId);
            if (1 != earnSpeedFundMark) {//未购买赚钱基金
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
            long earnSpeedPlusFundMark = GamePublicFunction.getEarnSpeedPlusFundMark(player, userId);
            if (1 == earnSpeedPlusFundMark) {
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        }else if (mallInfo.getMallId() == GameConfig.ACTIVITY_BEAUTY_FUND) {//美女基金
            if(NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(), eWelfareActivityType.BeautyFund.getValue()).size() <= 0){//没开活动
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            long beautyFundMark = GamePublicFunction.getFundMark(userId, ePlayerAttrType.BeautyFund,GameConfig.ACTIVITY_BEAUTY_FUND);
            if (1 == beautyFundMark) {//限购，美女基金已购买
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_BEAUTY_HIGHER_FUND) {//倾城基金
            if(NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(), eWelfareActivityType.BeautyHigherFund.getValue()).size() <= 0){//没开活动
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            long earnSpeedFundMark = GamePublicFunction.getFundMark(userId,ePlayerAttrType.BeautyFund,GameConfig.ACTIVITY_BEAUTY_FUND);
            if (1 != earnSpeedFundMark) {//未购买美女基金
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
            long earnSpeedPlusFundMark = GamePublicFunction.getFundMark(userId,ePlayerAttrType.BeautyHigherFund,GameConfig.ACTIVITY_BEAUTY_HIGHER_FUND);
            if (1 == earnSpeedPlusFundMark) {
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_CRICKET_FUND) {//促织基金
            if(NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(), eWelfareActivityType.CricketFund.getValue()).size() <= 0){//没开活动
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            long cricketFundMark = GamePublicFunction.getFundMark(userId, ePlayerAttrType.CricketFound, GameConfig.ACTIVITY_CRICKET_FUND);
            if (1 == cricketFundMark) {//限购，促织基金已购买
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        } else if (mallInfo.getMallId() == GameConfig.ACTIVITY_PATRON_LEVEL_FUND) {//门客等级基金
            if(NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(), eWelfareActivityType.PatronsLevelFund.getValue()).size() <= 0){//没开活动
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            long PatronsLevelFundMark = GamePublicFunction.getFundMark(userId, ePlayerAttrType.PatronsLevelFund, GameConfig.ACTIVITY_PATRON_LEVEL_FUND);
            if (1 == PatronsLevelFundMark) {//限购,门客等级基金已购买
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }

            List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.WelfareActivity.getValue(),eWelfareActivityType.PatronsLevelFund.getValue());
            if (openActivityInfoList.isEmpty()){
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
            ActivityInfo openActivityInfo = openActivityInfoList.get(0);

            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            if(null == player){
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
            String param6 = openActivityInfo.getParam6();
            int patronsId = StringUtils.string2intSafe(param6, 0);
            if (patronsId <= 0){
                logger.error("门客等级基金活动id = {}配置门客不存在",openActivityInfo.getActivityId());
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
            if (userPatrons == null){
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        } else {
            return GameErrorCode.E_MALL_NO_EXIST;
        }
        return 0;
    }
}
